home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group93c.txt / 000042_icon-group-sender _Tue Sep 7 00:30:31 1993.msg < prev    next >
Internet Message Format  |  1994-02-02  |  2KB

  1. Received: from owl.CS.Arizona.EDU by cheltenham.cs.arizona.edu; Tue, 7 Sep 1993 08:22:43 MST
  2. Received: by owl.cs.arizona.edu; Tue, 7 Sep 1993 08:22:42 MST
  3. Date: Tue, 7 Sep 93 00:30:31 -0700
  4. From: "Nevin Jerome Liber" <nevin>
  5. Message-Id: <9309070730.AA18987@caslon.CS.Arizona.EDU>
  6. To: icon-group@cs.arizona.edu
  7. Subject: Re: return results from procedure
  8. Status: R
  9. Errors-To: icon-group-errors@cs.arizona.edu
  10.  
  11. rjhare@festival.ed.ac.uk (Roger Hare) writes:
  12.  
  13. > I've been using Icon for seberal years now, but recently I stumbled across a
  14. > 'problem' for the fist time - I wanted to return more than one result from a
  15. > procedure (yup - strange to relate, after several years I had never needed to
  16. > do this before).
  17.  
  18. > What I want to know is "what is the 'best' way to return several results from
  19. > a procedure?'.
  20.  
  21. What I find most idiomatic for returning multiple results from a
  22. procedure is to use generators.  The called routine would look like:
  23.  
  24. procedure MyProcedure()
  25.  
  26.     ...
  27.     suspend xResult1
  28.     ...
  29.     suspend xResult2
  30.     ...
  31.     ...
  32.     suspend xResultN
  33.     ...
  34.  
  35. end
  36.  
  37. And the caller would look like:
  38.  
  39. ...
  40. every xElement := MyProcedure() do {
  41.     ProcessElement(xElement)
  42. }
  43. ...
  44.  
  45. This approach is space-efficient (if the data is being used in a FIFO
  46. order, only one element has to be in existence at a given time) and
  47. very flexible.  The caller, and not the callee can determine what type
  48. of data structure to keep the elements in (unlike in more traditional
  49. languages like C and Pascal).  For example, if I needed to build both an
  50. ordered list and a set, I could write the caller like:
  51.  
  52. LList := list()
  53. SSet := set()
  54.  
  55. every xElement := MyProcedure() do {
  56.     put(LList, xElement)
  57.     insert(SSet, xElement)
  58. }
  59. ___
  60.     Nevin ":-)" Liber    nevin@cs.arizona.edu    (602) 293-2799
  61.